<?php
  require_once("customize.inc.php");
  require_once("checksession.inc.php");
  require_once("comum.inc.php");
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Mantis Scrum Board - <?php echo(VERSAO) ?> - Tela principal</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--<link rel="stylesheet" href="css/scrum.css" type="text/css">-->
    <link rel="stylesheet" href="css/prototip.css" type="text/css">
    <link rel="stylesheet" href="css/progress.css" type="text/css">
    <style type="text/css">
div.minipostit {
    background-color: #FFFF80;
    border: 1px solid #000000;
    color: #000000;
    width: 62px;
    height: 62px;
    font-weight: bold;
    text-align: center;
    vertical-align: middle;
    float: left;
    margin: 1px;
    font-family: Arial;
}

table.miniPostit {
  height: 100%;
  width: 100%;
}

td.miniNomeSistema {
  font-size: 6pt;
  font-weight: bold;
  height: 33%;
  cursor: default;
}

td.miniNumeroCaso {
  font-size: 12pt;
  font-weight: bold;
  height: 33%;
}

td.miniPontuacao {
  font-size: 7pt;
  font-weight: bold;
  width: 50%;
  height: 33%;
  cursor: default;
  background-repeat: no-repeat;
  background-position: 44px 3px;
}

div.off td.miniPontuacao {
  background-image: url("img/pri-off.png");
}

div.p4 td.miniPontuacao {
  background-image: url("img/pri-4.png");
}

div.p3 td.miniPontuacao {
  background-image: url("img/pri-3.png");
}

div.p2 td.miniPontuacao {
  background-image: url("img/pri-2.png");
}

div.p1 td.miniPontuacao {
  background-image: url("img/pri-1.png");
}

    
    </style>
    <script type="text/javascript" src="js/ptt/prototype.js"></script>
    <script type="text/javascript" src="js/sct/scriptaculous.js"></script>
    <script type="text/javascript" src="js/prototip/prototip.js"></script>
    <script type="text/javascript" src="js/progress/progress.js"></script>
    <script type="text/javascript">
      Event.observe(document
                   ,"dom:loaded"
                   ,function() { 
                     Sortable.create("ordemdeexecucao", {tag:"div"
                                                        ,overlap: "horizontal"
                                                        ,constraint:"horizontal"
                                                        ,onUpdate: function() {
                                                          alert(Sortable.serialize("ordemdeexecucao"));
                                                        }});
                   });
    </script>
  </head>
  <body>

<?php
  
  
  require_once( '../mantistjpe/core.php' );

  $t_core_path = config_get( 'core_path' );

//  require_once( $t_core_path.'ajax_api.php' );
//  require_once( $t_core_path.'bug_api.php' );
  require_once( $t_core_path.'custom_field_api.php' );
  require_once( $t_core_path.'authentication_api.php' );  
//  require_once( $t_core_path.'date_api.php' );
//  require_once( $t_core_path.'last_visited_api.php' );

  //require_once("../mantistjpe/core/authentication_api.php");
  
 
  # --------------------
  # Check that there is a user logged-in and authenticated
  #  If the user's account is disabled they will be logged out
  #  If there is no user logged in, redirect to the login page
  #  If parameter is given it is used as a URL to redirect to following
  #   successful login.  If none is given, the URL of the current page is used
  function auth_ensure_user_authenticated2( $p_return_page = '' ) {
    # if logged in
    if ( auth_is_user_authenticated() ) {
      # check for access enabled
      #  This also makes sure the cookie is valid
      if ( OFF == current_user_get_field( 'enabled' ) ) {
        print_header_redirect( '../mantistjpe/logout_page.php' );
      }
    } else { # not logged in
      if ( is_blank( $p_return_page ) ) {
        if (!isset($_SERVER['REQUEST_URI'])) {
          $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
        }
        $p_return_page = $_SERVER['REQUEST_URI'];
      }
      $p_return_page = string_url( $p_return_page );
      print_header_redirect( '../mantistjpe/login_page.php?return=' . $p_return_page );
    }
  }
  
  auth_ensure_user_authenticated2();
  
  $accessLevel = get_enum_element("access_levels", current_user_get_access_level());
  
  if ($accessLevel < 70) {
    echo("Seu usuário não tem permissão para acessar este serviço");
  }       
  $userName = current_user_get_field("username");

  //$date = date( config_get("complete_date_format"));
  $realName = current_user_get_field("realname");
  
  
  
  printf("Acessando como: %s (%s)",$userName,is_blank($realName) ? $accessLevel : $realName . " - " . $accessLevel);

  echo("<br/>");
  
  echo("1. Verificar se o usuário logado tem a permissão para alterar o campo customizado<br/>\n");
  echo("2. Listar todos os casos do backlog usando a api do javascript para renderizar a lista alterável<br/>\n");
  
  $query = <<<EOS
  SELECT BUT.id AS caso
       , PRT.name AS sistema
       , COALESCE((SELECT COALESCE(CFS.value,CFT.default_value) FROM mantis_custom_field_string_table CFS JOIN mantis_custom_field_table CFT ON CFT.id = CFS.field_id WHERE CFS.field_id = %u AND CFS.bug_id = BUT.id),'0') AS storypoints
       , REPLACE(COALESCE((SELECT COALESCE(CFS.value,CFT.default_value) FROM mantis_custom_field_string_table CFS JOIN mantis_custom_field_table CFT ON CFT.id = CFS.field_id WHERE CFS.field_id = %u AND CFS.bug_id = BUT.id),'0'),'%%','') AS completude
       , COALESCE(BUT.target_version,'0.0.0.0') AS versao
       , False AS offsprint
       , BUT.priority AS prioridade
       , BUT.date_submitted AS dataenvio
    FROM mantis_bug_table BUT
    JOIN mantis_project_table PRT ON PRT.id = BUT.project_id
   WHERE BUT.handler_id = %u
     AND BUT.project_id <> %u
     AND NOT filhodepaidesprint(BUT.id,%u,%u)
     AND BUT.status = %u
EOS;
    
  $postits = executarSQL(sprintf($query
                                // BACKLOG
                                ,ID_CAMPO_STORYPOINT
                                ,ID_CAMPO_COMPLETUDE
                                // WHERE
                                ,$SESS_UNIDADE->getIdUsuario()
                                ,ID_PROJETO_PAI
                                ,$SESS_UNIDADE->getIdUsuario()
                                ,ID_PROJETO_PAI
                                ,ID_STATUS_A_EXECUTAR));
  
  $miniPostitTemplate = <<<EOH
  <div id="ac%s" style="display: none">%s</div>
  <table cellspacing="0" cellpadding="0" border="0" class="miniPostit">
    <tr><td class="miniNomeSistema">%s</td></tr>
    <tr><td class="miniNumeroCaso"><a href="%s/view.php?id=%s" style="text-decoration: none;" target="_new">%s</a></td></tr>
    <tr><td class="miniPontuacao">%s SP</td></tr>
  </table>
EOH;
  echo("<div id=\"ordemdeexecucao\">\n");
  $i = 0;
  while ($linha = obterRegistro($postits)) {
    if ($linha->offsprint) {
      $prioridade = "off";    
    } else {
      switch ($linha->prioridade) {
        case 20: $prioridade = "p1";
        break;
        case 30: $prioridade = "p2";
        break;
        case 40: $prioridade = "p3";
        break;
        case 50: $prioridade = "p4";
        break;
        default: $prioridade = "";
      }
    }
    $i++;
    echo("<div class=\"minipostit $prioridade\" id=\"c$linha->caso"."_$i\">".sprintf($miniPostitTemplate,$linha->caso,json_encode($linha),acertarNomeSistema($linha->sistema,$SESS_UNIDADE->getPrefixosRemoviveis(),$SESS_UNIDADE->getAbreviaturas()),ROOT_MANTIS,$linha->caso,$linha->caso,$linha->storypoints)."</div>\n");
  }
  echo("</div>\n");
?>
  <a href="javascript:ordem."></a>
  </body>
</html>
